package tools.encrypt;

/**
 * @Author: SunAo
 * @Date: 2020/11/20 16:32
 * @Description:
 */
public class SensitiveConvert {

    //是否敏感
    public static final Integer IS_SENSITIVE = 1;
    public static final String FOUR = "****";
    public static final String SIX = "******";

    /**
     * 手机号透明
     * @param type
     * @param telephone
     * @return
     */
    public static String convertMobile(Integer type,String mobile){
        if( IS_SENSITIVE.equals(type) ){
            if( mobile == null || "".equals(mobile) ){
                return null;
            }
            if( mobile.length() == 11 ){
                return mobile.substring(0, 3) + FOUR + mobile.substring(7);
            }
            if( mobile.length() > 11 ){
                return mobile.substring(0, 3) + FOUR + mobile.substring(mobile.length()-4);
            }
            if( mobile.length() < 11 ){
                if( mobile.length() <= 2 ){
                    return FOUR;
                }
                if( mobile.length() > 2 ){
                    return mobile.substring(0,1) + FOUR + mobile.substring(mobile.length()-1);
                }
            }
        }
        return mobile;
    }

    /**
     * 地址透明
     * @param type
     * @param address
     * @return
     */
    public static String convertAddress(Integer type,String address){
        if( IS_SENSITIVE.equals(type) ){
            return SIX;
        }
        return address;
    }

    /**
     * 邮箱透明
     * @param type
     * @param email
     * @return
     */
    public static String convertEmail(Integer type,String email){
        if( IS_SENSITIVE.equals(type) ){
            if( email == null || "".equals(email) ){
                return SIX;
            }
            int lastIndexOf = email.lastIndexOf("@");
            if( lastIndexOf == -1 ){
                return SIX;
            }
            String suffix = email.substring(lastIndexOf);
            String value = email.substring(0, lastIndexOf);
            if( value.length() == 1 ){
                return "*" + suffix;
            }
            if( value.length() == 2 ){
                return value.substring(0,1) + "*" + suffix;
            }
            if( value.length() >= 3 ){
                return value.subSequence(0, 1) + SIX + value.substring(value.length() - 1) + suffix;
            }
        }
        return email;
    }

    /**
     * 固定电话透明
     * @param type
     * @param telephone
     * @return
     */
    public static String convertTelephone(Integer type ,String telephone){
        if( IS_SENSITIVE.equals(type) ){
            if( telephone == null || "".equals(telephone) ){
                return FOUR;
            }

            if( telephone.length() <= 4 ){
                StringBuffer buffer = new StringBuffer();
                for( int i = 0 ; i < telephone.length() ; i++ ){
                    buffer.append("*");
                }
                return buffer.toString();
            }

            String start = telephone.substring(0, telephone.length() - 4);
            String end = telephone.substring(telephone.length() - 4);
            StringBuffer buffer = new StringBuffer();
            for( int i = 0 ; i < start.length() ; i++ ){
                buffer.append("*");
            }
            return buffer.append(end).toString();
        }
        return telephone;
    }

    /**
     * 微信号透明
     * @param type
     * @param wx
     * @return
     */
    public static String convertWx(Integer type,String wx){
        if( IS_SENSITIVE.equals(type) ){
            if( wx == null || "".equals(wx) ){
                return SIX;
            }
            if( wx.length() == 1 ){
                return wx + SIX;
            }
            if( wx.length() == 2 ){
                return wx.substring(0,1) + SIX + wx.substring(1);
            }
            return wx.substring(0,1) + SIX + wx.substring(wx.length() - 1);
        }
        return wx;
    }

    /**
     * QQ号透明
     * @param type
     * @param qq
     * @return
     */
    public static String convertQq(Integer type,String qq){
        if( IS_SENSITIVE.equals(type) ){
            if( qq == null || "".equals(qq) ){
                return SIX;
            }
            if( qq.length() == 1 ){
                return qq + SIX;
            }
            if( qq.length() == 2 ){
                return qq.substring(0,1) + SIX + qq.substring(1);
            }
            return qq.substring(0,1) + SIX + qq.substring(qq.length() - 1);
        }
        return qq;
    }

    /**
     * 企业微信透明
     * @param type
     * @param qywx
     * @return
     */
    public static String convertQywx(Integer type,String qywx){
        if( IS_SENSITIVE.equals(type) ){
            if( qywx == null || "".equals(qywx) ){
                return SIX;
            }
            if( qywx.length() == 1 ){
                return qywx + SIX;
            }
            if( qywx.length() == 2 ){
                return qywx.substring(0,1) + SIX + qywx.substring(1);
            }
            return qywx.substring(0,1) + SIX + qywx.substring(qywx.length() - 1);
        }
        return qywx;
    }


}
